package cn.jy.careuplusdoctor.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

public class BitmapUtil {

	/**
	 * 按比例缩放bitmap
	 * @param bgimage
	 * @param newWidth
	 * @param newHeight
	 * @return
	 */
    public static Bitmap zoomImage(Bitmap bitmap, int width,int height) {
    	if(bitmap==null || width <1 || height<1){
    		return bitmap;
    	}
    	bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
		return bitmap;
    }
    
    /**
     * 按比例缩放bitmap
     * @param bitmap
     * @param width
     * @return
     */
    public static Bitmap zoomImage(Bitmap bitmap, int width){
    	if(bitmap==null || width <1){
    		return bitmap;
    	}
    	int _w = bitmap.getWidth();
    	int _h = bitmap.getHeight();
    	float _d = ((float)_h)/((float)_w);
    	_h = (int) (width*_d);
    	return zoomImage(bitmap, width, _h);
    }
	
    
    public static File saveFile(Bitmap bitmap,File file){
    	if(file==null || bitmap==null){
    		return null;
    	}
    	try {
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
        	bitmap.compress(CompressFormat.JPEG, 60, bos);
        	byte[] bytes = bos.toByteArray();
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(bytes);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    	return file;
    }
}
